package com.dfrz.problem.action.staffmanage;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.dispatcher.SessionMap;

import com.dfrz.problem.model.Employee;
import com.dfrz.problem.model.Role;
import com.dfrz.problem.model.User;
import com.dfrz.problem.service.EmployeeService;
import com.dfrz.problem.service.RegisterService;
import com.dfrz.problem.service.RoleService;
import com.dfrz.problem.utils.IConstant;
import com.dfrz.problem.utils.Result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private EmployeeService employeeService;
	private RoleService roleService;
	private RegisterService registerService;
	private User user;
	private List<Employee> listEmployee;
	private Map<Integer, String> mapRole;
	private Employee employee;
	private int employeeId;
    private String queryKey;// 员工查询关键字
    private String queryErro;// 查询错误提示
    private int begin;// 每页开始序号
    private int end;// 每页结束序号
    private int page;// 页码
    private int size;// 员工列表大小
    
    
    public String addEmployee(){
    	return SUCCESS;
    }
    
    public String checkAddEmployee(){
    	// 注册信息填写有误
		if(!checkRegisterInfo()){
			return INPUT;
		}
		
		Result<Employee> result = registerService.addEmployee(employee);
		// 账户已存在
		if(IConstant.EXISTION_ACCOUNT.equals(result.getCode())){
			addFieldError("user", getText(IConstant.EXISTION_ACCOUNT));
			return INPUT;
		}
    	return SUCCESS;
    }
    
    /**
	 * 检验注册信息
	 * @return
	 */
	public boolean checkRegisterInfo(){
		registerService.create();
		
		if(employee == null){
			addActionError(getText(IConstant.REGISTER_INFO_NOT_NULL));
			return false;
		}
		boolean isSuccess = true;
		if(StringUtils.isEmpty(employee.getEmployeeEmail())){
			addFieldError("employee.employeeEmail", "邮箱账户不能为空.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getEmployeeName())){
			addFieldError("employee.employeeName", "用户姓名不能为空.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getPassword()) || StringUtils.isEmpty(employee.getAgainPwd())){
			addFieldError("employee.password", "密码输入不能为空.");
			isSuccess = false;
		}
		if(!employee.getAgainPwd().equals(employee.getPassword())){
			addActionError("两次输入的密码不同.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getBirthday())){
			addFieldError("employee.birthday", "出生日期不能为空.");
			isSuccess = false;
		}
		if(StringUtils.isEmpty(employee.getContact())){
			addFieldError("employee.contact", "联系电话不能为空.");
			isSuccess = false;
		}
		if(employee.getSex() != 1 && employee.getSex() != 2){
			addFieldError("employee.set", "必须选择性别.");
			isSuccess = false;
		}
			
		return isSuccess;
	}
    
    /**
     * 员工详细信息
     * @return
     */
    public String employeeDetail(){
    	employee = employeeService.getEmployee(employeeId).getResult();
    	// 获取角色列表
    	List<Role> list = roleService.roleList();
    	mapRole = new HashMap<Integer, String>();
    	if(list != null){
    		Iterator<Role> it = list.iterator();
    		while(it.hasNext()){
    			Role role = it.next();
    			if(role.getRoleId() != 1 && role.getRoleName() != "未分配"){
    				mapRole.put(role.getRoleId(),role.getRoleName());				
    			}
    		}
    	}
    	return SUCCESS;
    }
	
    /**
     * 员工列表
     * @return
     */
	public String employeeList(){
		Result<List<Employee>> result = employeeService.getAllEmployee();
		
		listEmployee = result.getResult();
		// 默认每页显示10条数据
		begin = 0;
		end = 9;
		page = 1;
		size = getListEmployee().size();
		if(end >= size){
			end = size-1;
		}
		queryErro = "";
		
		return SUCCESS;
	}
	
	public String queryEmployee(){
		employeeList();
		if(queryKey.trim().length() == 0 || queryKey == null){
			queryErro = "请输入员工名称关键字!";
			return SUCCESS;
		}else{
			// 将包含查询关键字的问题保留
			Iterator<Employee> it = listEmployee.iterator();
			while(it.hasNext()){
				Employee employee = it.next();
				if(!employee.getEmployeeName().contains(queryKey)){
					it.remove();
				}
			}
			// 默认每页显示10条数据
			begin = 0;
			end = 9;
			page = 1;
			size = getListEmployee().size();
			if(end >= size){
				end = size-1;
			}
		}
		
		if(listEmployee.size() == 0){
			employeeList();
			queryErro = "该员工不存在!";
		}
		
		return SUCCESS;
	}
	
	/**
	 * 查询下一页
	 * @return
	 */
	public String nextPage(){
		begin = end+1;
		end = begin+9;
		size = getListEmployee().size();
		if(end >= size){
			end = size-1;
		}
		if(begin >= size){
			begin = size-1;
			end = begin;
		}
		page ++;
		if(page >= (size-1)/10+1){
			page = (size-1)/10+1;
		}
		
		return SUCCESS;
	}
	
	/**
	 * 查询上一页
	 * @return
	 */
	public String prePage(){
		begin -= 10;
		if(begin <= 0){
			begin = 0;
		}
		end = begin + 9;
		if(end >= size){
			end = size-1;
		}
		page --;
		if(page <= 0){
			page = 1;
		}
		
		return SUCCESS;
	}
	
	/**
	 * 跳转至指定页面
	 * @return
	 */
	public String toPage(){
		queryErro = "";
		// 超过最大页数时跳转回首页
		if(page > size/10+1){
			page = 1;
		}
		begin = (page-1)*10;
		end = begin+9;
		if(end >= size){
			end = size-1;
		}
		
		return SUCCESS;
	}
	
	////////////////////////////
	public List<Employee> getListEmployee() {
		return listEmployee;
	}
	
	public int getBegin() {
		return begin;
	}
	public void setBegin(int begin) {
		this.begin = begin;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	public String getQueryErro() {
		return queryErro;
	}
	
	public void setQueryKey(String queryKey) {
		this.queryKey = queryKey;
	}
	
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public Employee getEmployee() {
		return employee;
	}
	
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}
	
	public User getUser() {
		SessionMap<String, Object> session = (SessionMap<String, Object>) ActionContext.getContext().getSession();
		user = (User) session.get("user");
		return user;
	}
	
	public void setRegisterService(RegisterService registerService) {
		this.registerService = registerService;
	}
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public Map<Integer, String> getMapRole() {
		return mapRole;
	}
}
